package com.beizhen.demo.controller;
import com.beizhen.demo.entity.StuAdministrativeDivision;
import com.beizhen.demo.entity.StuBase;
import com.beizhen.demo.entity.StuBaseClassify;
import com.beizhen.demo.entity.StuDictionaries;
import com.beizhen.demo.service.StuAdministrativeDivisionService;
import com.beizhen.demo.service.StuBaseClassifyService;
import com.beizhen.demo.service.StuBaseService;
import com.beizhen.demo.service.StuDictionariesService;
import com.beizhen.demo.utils.Fileupload;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/** 基地表 前端控制器*/
    @Controller
    @RequestMapping("/stuBase")
    public class StuBaseController {
    @Autowired
    private StuBaseService stuBaseService;
    @Autowired
    private StuDictionariesService dictionariesService;
    @Autowired
    private StuBaseClassifyService stuBaseClassifyService;
    @Autowired
    private StuAdministrativeDivisionService stuAdministrativeDivisionService;

    /**
     * 袁康飞25-81行
     * @return
     */
    //跳转到餐饮页面
    @RequestMapping("/goRepast")
    public String goRepast(){
        return "base_administration_catering";
    }

    //跳转到概况页面
    @RequestMapping("/goProfiles")
    public String goProfiles(){
        return "base_administration_general";
    }
    //跳转到接待页面
    @RequestMapping("/goReception")
    public String goReception(){
        return "base_administration_reception";
    }

    //更新概况详情
    @RequestMapping("/updProfiles")
    public String updProfiles(@RequestParam(value = "b_id") Integer b_id,
                              @RequestParam(value = "content") String b_baseProfiles,
                              @RequestParam(value = "b_baseProfilesPicture") MultipartFile b_baseProfilesPicture) throws IOException {
        //上传到服务器返回路径
        //String img = com.beizhen.demo.Utils.Fileupload.getUrl(b_baseProfilesPicture);
        Boolean flag = stuBaseService.update_Stubase_baseProfiles(b_id, b_baseProfiles, null);
        if(flag){
            return "base_administration_general";//如果更新成功跳转到成功页面
        }else {
            return "base_administration_reception";//如果更新失败跳转到失败页面
        }
    }

    //更新接待详情
    @RequestMapping("/updReception")
    public String updReception(@RequestParam(value = "b_id") Integer b_id,
                               @RequestParam(value = "b_baseReception") String b_baseReception,
                               @RequestParam(value = "b_baseReceptionPicture") String b_baseReceptionPicture){
        Boolean flag = stuBaseService.update_Stubase_baseReception(b_id, b_baseReception, b_baseReceptionPicture);
        if(flag){
            return "";//如果更新成功跳转到成功页面
        }else {
            return "";//如果更新失败跳转到失败页面
        }
    }

    //更新餐饮详情
    @RequestMapping("/updRepast")
    public String updRepast(@RequestParam(value = "b_id") Integer b_id,
                            @RequestParam(value = "b_baseRepast") String b_baseRepast,
                            @RequestParam(value = "b_baseRepastPicture") String b_baseRepastPicture){
        Boolean flag = stuBaseService.update_Stubase_baseRepast(b_id, b_baseRepast, b_baseRepastPicture);
        if(flag){
            return "";//如果更新成功跳转到成功页面
        }else {
            return "";//如果更新失败跳转到失败页面
        }
    }

    /**
     * 邓流洋 2020.08.01
     * 跳转到填写入驻审核信息的页面 106——204行
     */
    @RequestMapping("/toAddStuBase")
    public String toAddStuBase(HttpServletRequest request, Model model){
        //查询出路线特色
        List<StuDictionaries> lineFeature = dictionariesService.getLineFeature();
        //查询一级机构区划
        List<StuBaseClassify> oneAreaList = stuBaseClassifyService.getOneAreaInfo();
        //查询一级行政区划
        List<StuAdministrativeDivision> oneAdminList = stuAdministrativeDivisionService.getOneAreaInfo();
        /*model.addAttribute("lineFeatureList",lineFeature);
        model.addAttribute("oneAreaList",oneAreaList);*/
        model.addAttribute("lineFeatureList",lineFeature);
        model.addAttribute("oneAreaList",oneAreaList);
        model.addAttribute("oneAdminList",oneAdminList);
        return "base_enter_audit";
    }

    /*提交基地入驻审核*/
    @ResponseBody
    @RequestMapping(value = "/addStuBase",method = RequestMethod.POST)
    public String addStuBase(StuBase stuBase, HttpSession session, HttpServletRequest request, @RequestParam(value = "attachs",required = false) MultipartFile[] multipartFiles){
        System.out.println("来来来来来来");
        //审批文件
        String idPicPath="";
        //营业执照
        String workPicPath="";
        String picPath = "";
        String errorInfo="";
        for(int i=0;i<multipartFiles.length;i++){
            MultipartFile multipartFile=multipartFiles[i];
            if(!multipartFile.isEmpty()){
                if(i==0){
                    errorInfo="uploadFileError";
                }else if(i==1){
                    errorInfo="uploadWpError";
                }
                //上传前的准备工作
                //1、定义上传的目标路径
                String path=session.getServletContext().getRealPath("static"+ File.separator+"uploadfile");
                //2、获取源文件名
                String oldname=multipartFile.getOriginalFilename();
                //3、文件的后缀
                String suffix= FilenameUtils.getExtension(oldname);
                if(multipartFile.getSize()>5000000){
                    request.setAttribute(errorInfo,"文件的大小不能超过5M");
                    return "base_enter_audit";
                }else if(suffix.equalsIgnoreCase("jpg")||
                        suffix.equalsIgnoreCase("png")||
                        suffix.equalsIgnoreCase("jpeg")||
                        suffix.equalsIgnoreCase("pneg")||
                        suffix.equalsIgnoreCase("pdf")||
                        suffix.equalsIgnoreCase("docx")||
                        suffix.equalsIgnoreCase("doc")||
                        suffix.equalsIgnoreCase("zip")||
                        suffix.equalsIgnoreCase("rar")
                ){
                    //给文件起新名字
                    String filename=System.currentTimeMillis()+ RandomUtils.nextInt(1,1000000)+"."+suffix;
                    //创建目标文件
                    File newFile=new File(path,filename);
                    if(!newFile.exists()){
                        newFile.mkdirs();
                    }
                    //写入
                    try {
                        picPath = Fileupload.getUrl(multipartFile);
                    } catch (IOException e) {
                        e.printStackTrace();
                        request.setAttribute(errorInfo,"上传失败");
                        return "base_enter_audit";
                    }
                    if(i==0){

                        idPicPath=picPath;
                    }else if(i==1){
                        workPicPath=picPath;
                    }

                }else{
                    request.setAttribute(errorInfo,"上传失败");
                    return "base_enter_audit";
                }
            }
        }
        stuBase.setBClearfile(idPicPath);
        stuBase.setBBusinesslicense(workPicPath);

        /*stuBase.setBStarttime(new Date());*/
        if(stuBaseService.addStuBaseInfo(stuBase)>0){
            return "ttt";
        }else{
            return "base_enter_audit";
        }
    }
}
